# coding: utf-8
from pymysql import connect

class MySQLDemo:
    def __init__(self):
        self.conn = connect('192.168.43.158', 'guest', 'guest', 'lz_py', 3306, charset='utf8')
        self.cs = self.conn.cursor()

    def _test_add(self):
        """
        md = MySQLDemo()
        md._test_add()
        """
        # 增加数据
        sql = "insert into goods values(1, '笔记本', '3399'),(2, '洗衣机', '2555')"
        self.cs.execute(sql)
        self.conn.commit()
        sql = "select * from goods"
        self.cs.execute(sql)
        for items in self.cs.fetchall():
            print(items)

    def _test_update(self):
        """
        md = MySQLDemo()
        md._test_update()
        """
        # 修改数据
        sql = "update goods set name='电视机' where id between 1 and 2"
        self.cs.execute(sql)
        self.conn.commit()
        sql = "select * from goods"
        self.cs.execute(sql)
        for items in self.cs.fetchall():
            print(items)

    def _test_delete(self):
        """
        md = MySQLDemo()
        md._test_delete()
        """
        # 删除数据
        sql = "delete from goods"
        self.cs.execute(sql)
        self.conn.commit()
        sql = "select * from goods"
        self.cs.execute(sql)
        for items in self.cs.fetchall():
            print(items)

    def __del__(self):
        self.cs.close()
        self.conn.close()


if __name__ == '__main__':
    md = MySQLDemo()
    md._test_delete()
